//
//  SpecialViewController.m
//  花花时尚
//
//  Created by 霍文轩 on 15/8/26.
//  Copyright (c) 2015年 霍文轩. All rights reserved.
//

#import "DetailImageDownViewController.h"
#import "UIImageView+WebCache.h"
#import "NightMode.h"

@interface DetailImageDownViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic,retain) UIImageView * imageView;
@property (nonatomic,retain) UIImagePickerController * pick;
@end

@implementation DetailImageDownViewController
- (void)dealloc
{
    [_imageView release];
    [_pick release];
    [super dealloc];
}
-(instancetype)initWithImage:(UIImage *)mainImage imgRect:(CGRect)imgRect linkURL:(NSString *)linkURL{
    self = [super init];
    if (self) {
        self.view.backgroundColor = [UIColor blackColor];
        
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imgRect.size.width, imgRect.size.height)];
        _imageView.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
        [self.view addSubview:_imageView];
        _imageView.image = mainImage;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createViews];
}
-(void)createViews{
    self.pick = [[UIImagePickerController alloc] init];
    _pick.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    _pick.allowsEditing = YES; // 允许编辑
    [self presentViewController:_pick animated:YES completion:nil]; // 模态跳转
    _pick.delegate = self;
    [_pick release];
    
    // 长按手势
    UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(createAlert)];
    [self.view addGestureRecognizer:longPress];
    [longPress release];
}

#pragma mark - 长按手势，弹出警告框，保存到本地相册
-(void)createAlert{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * action = [UIAlertAction actionWithTitle:@"保存到本地" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if(self.imageView.image){
            UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);// 保存到相册。参数分别为要保存的图片，回调方法所在的对象，回调方法，在回调中可以选择上传的数据
        }
    }];
    
    UIAlertAction * cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }]; // 只能有一个Cancel类型
    
    [alert addAction:action];
    [alert addAction:cancle];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}

#pragma mark - 判断图片是否保存成功
- (void)image:(UIImage *)image
didFinishSavingWithError:(NSError *)error
  contextInfo:(void *)contextInfo{
    // 创建提示label
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
    label.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height-100);
    label.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    label.textColor = [UIColor whiteColor];
    label.layer.cornerRadius = 10;
    label.clipsToBounds = YES; // 设置这个才会使圆角生效
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont boldSystemFontOfSize:17];
    [self.view addSubview:label];
    // 1s后让label消失
    [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(labelDisappear:) userInfo:@{@"label":label} repeats:NO];
    [label release];
    if (error != NULL){
        //失败
        label.text = @"保存失败！无法访问相册";
    }
    else{
        //成功
        label.text = @"保存成功";
    }
}
// 让提示label消失
-(void)labelDisappear:(NSTimer *)timer{
    UILabel * label = [timer.userInfo objectForKey:@"label"];
    [label removeFromSuperview];
}
#pragma mark - 轻按退出
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self returnVC];
}
-(void)returnVC{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
@end
